/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.core;

import junit.framework.TestCase;

public class TagServiceTests extends TestCase {
    private TagService service;
    private Object object;
    
    protected void setUp() throws Exception {
        service = new TagService();
        object = new Object();
    }
    
    public void testPut() throws Exception {
        Tag tag = new Tag(Tags.ID, "value");
        assertEquals(0, service.getTagCount(object));
        Tag[] tags = service.getAll(object);
        assertNotNull(tags);
        assertEquals(0, tags.length);
        assertNull(service.put(object, tag));
        
        assertEquals(1, service.getTagCount(object));
        tags = service.getAll(object);
        
        assertNotNull(tags);
        assertEquals(1, tags.length);
        Tag returned = (Tag) tags[0];
        assertEquals(Tags.ID, returned.getName());
        assertEquals("value", returned.getValue());
        
        assertNotNull(service.put(object, tag));
    }
    
    public void testGet() throws Exception {
       String name = Tags.ID;
       Tag tag1 = new Tag(name, "value");
       Tag tag2 = new Tag(name, "value");
       
       service.put(object, tag1);
       service.put(object, tag2);
       assertEquals(1, service.getTagCount(object));
       
       assertEquals(tag1, service.get(object, name));
       assertEquals(tag2, service.get(object, name));
    }
    
    public void testRemove() throws Exception {
        String name = Tags.ID; 
        Tag tag = new Tag(name, "1");  
        
        service.put(object, tag);
        service.put(object, new Tag("stub", "stub")); 
        assertEquals(2, service.getTagCount(object));
        
        tag = service.remove(object, name);
        assertEquals(name, tag.getName());
        assertEquals(1, service.getTagCount(object));
    }
    
    public void testContains() throws Exception {
        assertFalse(service.contains(object, "name"));
        
        service.put(object, new Tag("name", "value"));
        assertTrue(service.contains(object, "name")); 
    }
}
